/**
 * 
 */
package com.pi.tools;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.mapdigit.gis.geometry.GeoLatLng;

/**
 * @author hxx 2012 05 29
 *广东省解析文件的协议接口方法
 */
public class GDGPSDataParserUtil extends GMapUtils implements IGPSDataParserUtil{
	public GDGPSDataParserUtil(){
		
		
	}
	@Override
	public List<String[]> ParseFile(String fileName) {
		 List<String[]> list = new ArrayList<String[]>();
		try {
			FileInputStream fis;
			fis = new FileInputStream(fileName);
			InputStreamReader isr;
			isr = new InputStreamReader(fis, "UTF-8");
	        BufferedReader br = new BufferedReader(isr);
            String line="";
			while ((line=br.readLine())!=null) {
			  if(line.length() >= CARS_LENGTH){
				  
				 String str[] = new String[9];
				 //此部分解析文件的文件内容保存到str[]变量中去
				 str[0] = getTime(line.substring(0, 12)); //时间
				 earth = new GeoLatLng( getLatitude(line.substring(20,28)), getlongitude(line.substring(12,20)));
				 GeoLatLng geoLatLng = chinaMapOffset.fromEarthToMars(earth);
		         str[1] = String.valueOf(geoLatLng.lng()); //经度
				 str[2] = String.valueOf(geoLatLng.lat());//纬度
				 str[3] = String.valueOf(getSpeed(line.substring(28,30)));//速度
				 str[4] = getDirection(line.substring(30,32));//方向
				 str[5] = String.valueOf(getHeight(line.substring(32,36)));//高度
				 str[6] = String.valueOf(getMileage(line.substring(36,44)));//里程
				 str[7] = getState(line.substring(44,60));//状态
				 
				 str[8] = String.valueOf(getDirectNotChage(line.substring(30,32)));//方向的编码
				 list.add(str);
			  }
			}
			 br.close();
	         isr.close();
	         fis.close();
		}catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		 return list;
	}

	@Override
	public String getDirection(String str) {
		
		return FormatGpsInfo.getDirection(Integer.parseInt(hexToString(str)));
	}
	@Override
	public Integer getDirectNotChage(String str) {
		
		return Integer.parseInt(hexToString(str));
	}
	@Override
	public Double getHeight(String str) {
		return Double.parseDouble(hexToString(str));
	}

	@Override
	public Double getLatitude(String str) {
		return getItude(str);
	}
	@Override
	public Double getlongitude(String str) {
		
		return getItude(str);
	}
	@Override
	public Double getMileage(String str) {
		 
		return Double.parseDouble(str);
	}

	@Override
	public Double getSpeed(String str) {
		return Double.parseDouble(hexToString(str));
	}

	@Override
	public String getState(String str) {
		String[][] cars = this.getCarstat(str);// 状态(2*8字节)
		String oil = cars[0][3], door = cars[1][1], acc = cars[1][5], motor = cars[3][2];
		return oil + "," + door + "," + acc + "," + motor;
	}

	@Override
	public String getTime(String str) {
		
		return "20" + str.substring(0,2) + "-" + str.substring(2,4)+"-" +str.substring(4, 6) + " " + str.substring(6, 8) + ":" + str.substring(8, 10)
	      + ":" + str.substring(10, 12);
	}
	
	
	public String[][] getCarstat(String stat) {
		String[][] stats = new String[4][8];
		long carstat = Long.parseLong(stat, 16);
		byte[] bytes = GPSUtilities.longToByteArray(carstat);
		StringBuffer buff = new StringBuffer();
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 8; j++) {
				byte bytex = GPSUtilities.getByteBit(bytes[i], j);
//				stats[i][j] = this.getString("car.carstat" + (i + 1) + j + bytex);
				stats[i][j] =  hashMapState.get("car.carstat" + (i + 1) + j + bytex);
				buff.append(bytex + "-");
				buff.append(stats[i][j] + ";");
			}
		}
		return stats;
	}
	
	/**
	 * 历史轨迹用
	 * 
	 * @param itude
	 * @return
	 */
	public double getItude(String itude) {
		int length = itude.length();
		StringBuffer append = new StringBuffer();
		for (int i = 0; i < 8 - length; i++) {
			append.append("0");
		}
		append.append(itude);
		String dest = append.toString();
		return itude(dest);
	}
	
	/**
	 * 计算十进制的经纬度
	 * 
	 * @param dest
	 *            0DDFF.FFF 采用BCD码
	 * @return
	 */
	private float itude(String dest) {
		String udex = dest.substring(0, 3);// 度
		String min0 = dest.substring(3, 5);// 分(小数点前二位)
		String second = dest.substring(5);// 分(小数点后三位)
		float minX = Float.parseFloat(min0 + "." + second) / 60;// 分换算成度
		float itude0 = Float.parseFloat(udex) + minX;
		return itude0;
	}
	
}
